#!/user/bin/env python3
# -*- coding: utf-8 -*-
import logging
from multiprocessing import Process, Lock

# 配置日志
logging.basicConfig(filename='example.log', level=logging.DEBUG)

# 创建一个锁对象
lock = Lock()


def write_log(message):
    # 获取锁
    with lock:
        logging.debug(message)


if __name__ == '__main__':
    # 创建多个进程
    processes = [Process(target=write_log, args=(f'这是来自进程 {i} 的日志',)) for i in range(5)]

    # 启动进程
    for p in processes:
        p.start()

    # 等待所有进程结束
    for p in processes:
        p.join()
